<?php

Yii::import('application.models._base.BaseUsers');

class Users extends BaseUsers {

    public $confirm_password;

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    public function rules() {
        return array(
            array('username,confirm_password, password', 'required'),
            array('username', 'unique'),
            array('confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => Yii::t("UserModule.user", "Password and Confirm Password are not same.")),
            array('createtime, lastvisit, superuser, status', 'numerical', 'integerOnly' => true),
            array('username', 'length', 'max' => 100),
            array('password, activationkey', 'length', 'max' => 128),
            array('app_id', 'default', 'setOnEmpty' => true, 'value' => null),
            array('id, app_id, username, password', 'safe', 'on' => 'search'),
        );
    }

    public function beforeSave() {
        $this->setAttribute('password', md5($this->getAttribute('password')));
        $this->setAttribute('createtime', time());
        return true;
    }

    public function saveUserRole($roleId) {
        $userRoles = new UserHasRole();
        $userRoles->user_id = $this->id;
        $userRoles->role_id = $roleId;
        $userRoles->save(false);
    }

    public static function changeCreateTime($time) {
        if (!empty($time)) {
            return date('m-d-Y h:i:s A', $time);
        }
    }
}
?>